package com.example.dynamicdatasource.interceptor;

import com.example.dynamicdatasource.dataSource.DynamicDataSourceHolder;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @author mqb
 * @date 2021/8/9 20:02
 */
@Slf4j
public class ThreadLocalRemoveInterceptor implements HandlerInterceptor {
    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
        String key = DynamicDataSourceHolder.get();
        DynamicDataSourceHolder.remove();
        log.info("remove current datasource in ThreadLocal: {}", key);
    }
}
